package cn.edu.ctbu.dp01.api;

import cn.edu.ctbu.dp01.entity.Student;
import cn.edu.ctbu.dp01.exception.RException;
import cn.edu.ctbu.dp01.service.StudentService;
import cn.edu.ctbu.dp01.util.RUtil;
import cn.edu.ctbu.dp01.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student")
public class StudentApiController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/list")
    public R<List<Student>> findAll() {

        List<Student> students = studentService.findAll();
        return RUtil.success(students);
    }

    @GetMapping("/{id}")
    public R<Student> findById(@PathVariable Integer id) {
        Student student = studentService.findById(id);
        return RUtil.success(student);
    }

    @GetMapping("/add")
    public R<Student> add(Student student) {

        return RUtil.success(studentService.add(student));
    }

    @PutMapping("/update")
    public R<Student> update(Student student) {

        return RUtil.success(studentService.upde(student));
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {

        studentService.delete(id);
        return RUtil.success();
    }

    @GetMapping("/validateUser")
    public R<Student> validateSnoAndPassword(String sno,String password) throws Exception {
        return RUtil.success(studentService.validateUsernameAndPassword(sno,password));
    }
}
